package com.cy.model;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cy.BasicModel;
import com.cy.ext.plugin.tablebind.TableBind;

import java.util.List;

@TableBind(tableName="menu",pkName = "id")
public class Menu extends BasicModel<Menu> {

    public static final Menu dao = new Menu();
    public static final String SELECT_ALL = "`id`,`name`,`url`,`method`,`type`,`parent_id`,`platform`";

    public enum Type{
        PLATFROM("平台资源"),
        DINGTALK_MOBILITY("钉钉资源(移动端)"),
        DINGTALK_PC("钉钉资源(PC端)");

        private String name;

        Type(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public static JSONArray toJson(){
            JSONArray result = new JSONArray(values().length);
            for (Type item : values()){
                JSONObject jsonObject = new JSONObject(2);
                jsonObject.put("type", item.ordinal());
                jsonObject.put("text", item.getName());
                result.add(jsonObject);
            }
            return result;
        }
    }

    @Override
    public boolean save() {
        setPlatform(CCRM);
        return super.save();
    }

    public List<Menu> findAllByType(Integer type) {
        StringBuilder sql = new StringBuilder(20);
        sql.append("SELECT * FROM menu WHERE platform & "+ CCRM +" = "+ CCRM);
        if (type != null)
            sql.append(" AND type ="+ type);
        return find(sql.toString());
    }

    /**
     * 根据父ID查找
     * @param parentId
     * @return
     */
    public List<Menu> findByParent(Long parentId, Integer type){
        StringBuilder sql = new StringBuilder(30);
        sql.append("SELECT * FROM menu WHERE platform & "+ CCRM +" = "+ CCRM);
        sql.append(parentId == null ? " AND parent_id IS NULL" : " AND parent_id = "+ parentId);
        if (type != null)
            sql.append(" AND type = "+ type);
        return find(sql.toString());
    }

    /**
     * 根据角色查询菜单（权限）
     */
    public List<Menu> findByRoleAndType(Long roleId, Integer type) {
        return find("SELECT m.* FROM permission p ,menu m WHERE p.menu_id = m.id AND m.platform & "+ CCRM +" = "+ CCRM +" AND p.account_role_id = ? AND m.type = ?", roleId, type);
    }

    /**
     * 根据角色查询菜单（权限）
     */
    public List<Menu> findByRole(Long roleId) {
        return find("SELECT m.* FROM permission p ,menu m WHERE p.menu_id = m.id AND m.platform & "+ CCRM +" = "+ CCRM +" AND p.account_role_id = ?", roleId);
    }

    /**
     * 级联删除，删除下级所有子菜单
     */
    public void deleteCascade() {
        List<Menu> children = getChildren();
        for (Menu child : children){
            child.deleteCascade();
        }
        delete();
    }

    /**
     * 级联更新菜单类型
     */
    public void updateTypeCascade(){
        List<Menu> children = getChildren();
        for (Menu child : children)
            child.updateTypeCascade_1(this);
    }

    /**
     * 递归方法
     */
    private void updateTypeCascade_1(Menu root){
        List<Menu> children = getChildren();
        for (Menu child : children){
            child.updateTypeCascade_1(root);
        }
        setType(root.getType()).update();
    }

    /**
     * 获取子菜单(无限层级)
     */
    public List<Menu> getAllChildren(){
        List<Menu> subs = getChildren();
        if (subs.size() > 0){
            for (Menu sub : subs){
                List<Menu> children = sub.getAllChildren();
                if (children.size() > 0){
                    sub.put("children", children);
                }
            }
        }
        return subs;
    }

    /**
     * 获取子菜单(下一级)
     */
    public List<Menu> getChildren(){
        return find("SELECT * FROM menu WHERE platform & "+ CCRM +" = "+ CCRM +" AND parent_id = ?", getId());
    }

    public Menu setId(Long id){
        return this.set("id",id);
    }

    public Long getId(){
        return this.get("id");
    }
        
    public Menu setName(String name){
        return this.set("name",name);
    }

    public String getName(){
        return this.getStr("name");
    }
        
    public Menu setUrl(String url){
        return this.set("url",url);
    }

    public String getUrl(){
        return this.getStr("url");
    }

    public Menu setMethod(String method){
        return this.set("method",method);
    }

    public String getMethod(){
        return this.getStr("method");
    }

    public Menu setType(Integer type){
        return this.set("type",type);
    }

    public Integer getType(){
        return this.get("type");
    }
        
    public Menu setParentId(Long parentId){
        return this.set("parent_id",parentId);
    }

    public Long getParentId(){
        return this.get("parent_id");
    }

    public Menu setPlatform(Integer platform){
        return this.set("platform",platform);
    }

    public Integer getPlatform(){
        return this.get("platform");
    }

}